# -*- coding:utf-8 -*-
"""
Author      : Xiaobaogua
Date        : 2023-12-14
Update      : 2023-12-18
File Name   : AssetQuery.py
"""
import sys
import json
import cmdb_query
import re


def AssetQuery(search_ip):
    # 对于IP进行判断，如果不是内网IP不执行脚本
    if not re.match("^10\\.||^192\\.", search_ip):
        return {"message": "已跳过查询"}

    # 先查询CMDB
    result = cmdb_query.asset_query(search_ip)
    if result["message"] == "success":
        result["search_ip"] = result["result"]["search_ip"]
        result["hostname"] = result["result"]["hostname"]
        result["name"] = result["result"]["name"]
        result["group"] = result["result"]["group"]
    else:
        result["msg"] = "查无数据"

    return result


if __name__ == "__main__":
    args = sys.argv
    if len(args) > 1:
        search_ip = args[1]
        search_result = AssetQuery(search_ip)
        print(json.dumps(search_result, ensure_ascii=False))
